package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.ContainerMapper;
import com.it.mapper.ProductMapper;
import com.it.mapper.WasteMapper;
import com.it.mapper.WasteTypeMapper;
import com.it.pojo.*;
import com.it.pojo.dto.ProductDto;
import com.it.pojo.enums.InstoreStatus;
import com.it.service.ProductService;
import com.it.service.WasteTypeService;
import com.it.util.CodeUtils;
import com.it.util.TimeUtils;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/25 10:51
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private WasteTypeService wasteTypeService;

    @Autowired
    private ContainerMapper containerMapper;

    @Autowired
    private WasteMapper wasteMapper;

    @Autowired
    private WasteTypeMapper wasteTypeMapper;

    /*获取添加时的主键id*/
    public String getPrimaryKey() {
        ProductExample example = new ProductExample();
        List<Product> list = productMapper.selectByExample(example);
        return list.size() + 1 + "";
    }

    @Override
    public String insertProduct(Product product) {
        String msg = "";
        product.setProductId(getPrimaryKey());//主键
        product.setProductDel(0);//未删除
        product.setProductTime(new Date());//当前日期
        product.setProductStatus(1);//默认未入库
        product.setProductCode(CodeUtils.getCodeByCountCF(Integer.parseInt(getPrimaryKey())));//产废编码
        WasteType wasteType = wasteTypeMapper.selectByPrimaryKey(product.getTypeId());
        product.setTypeName(wasteType.getTypeName());//废物类别
        Container container = containerMapper.selectByPrimaryKey(product.getContainerId());
        product.setContainerCode(container.getContainerCode());//容器编码
        container.setCompanyId(product.getCompanyId());//公司id
        if (container.getWasteId() != null) {
            //该容器已经存储了废物
            Waste waste = wasteMapper.selectByPrimaryKey(container.getWasteId());
            WasteType type = wasteTypeService.findById(waste.getTypeId());
            if (product.getTypeId().equals(type.getTypeId())) {
                //存储的是同一种类型
                msg = "同一种类型";
            } else {
                return "容器只能存储一种类型的废物，请重新选择容器";
            }
        } else {
            //该容器还未存储废物
            container.setWasteId(product.getWasteId());
        }

        if (product.getProductNum() > (container.getContainerMaxvolume() - container.getContainerRealvolume())) {
            //容器装不下了
            msg = "容器已经满了,请选择较大的容器";
        } else {
            //实际容量添加
            int realVolume = product.getProductNum().intValue() + container.getContainerRealvolume();
            container.setContainerRealvolume(realVolume);

            containerMapper.updateByPrimaryKeySelective(container);
            productMapper.insertSelective(product);
            /*产废成功后删除容器*/
            container.setContainerDel(1);
            container.setContainerStatus(1);//被拉走
            containerMapper.updateByPrimaryKeySelective(container);
            msg = "产废成功";
        }
        return msg;
    }

    @Override
    public List<Product> getProducts(String companyid) {
        return productMapper.findAllByCompanyIdAndProductStatusProducts(companyid, InstoreStatus.NOINSTORE.getStatusNum());
    }

    @Override
    public Product getProduct(String productid) {
        return productMapper.selectByPrimaryKey(productid);
    }

    /*产废表条件+分页查询*/
    @Override
    public PageInfo<Product> findPage(ProductDto dto) {
        if (dto.getPageNo() == null) {
            dto.setPageNo(1);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andProductTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andProductTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andProductTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andProductTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("product_time asc");
        List<Product> list = productMapper.selectByExample(example);
        PageInfo<Product> pageInfo = new PageInfo<>(list, 5);
        return pageInfo;
    }

    /*根据条件导出excel*/
    @Override
    public List<Product> findExcel(ProductDto dto) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andProductTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andProductTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andProductTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andProductTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("product_time asc");
        return productMapper.selectByExample(example);
    }

}
